<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="采收日期" prop="pickTime">
        <el-date-picker clearable
          v-model="queryParams.pickTime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择采收日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="地块编号" prop="plotId">
        <el-input
          v-model="queryParams.plotId"
          placeholder="请输入地块编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单号" prop="orderNum">
        <el-input
          v-model="queryParams.orderNum"
          placeholder="请输入单号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="采收品种" prop="pickSort">
        <el-input
          v-model="queryParams.pickSort"
          placeholder="请输入采收品种"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="毛重" prop="grossWt">
        <el-input
          v-model="queryParams.grossWt"
          placeholder="请输入毛重"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="皮重" prop="tareWt">
        <el-input
          v-model="queryParams.tareWt"
          placeholder="请输入皮重"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="净重" prop="netWt">
        <el-input
          v-model="queryParams.netWt"
          placeholder="请输入净重"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="扣杂" prop="lessWt">
        <el-input
          v-model="queryParams.lessWt"
          placeholder="请输入扣杂"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="糖度" prop="brix">
        <el-input
          v-model="queryParams.brix"
          placeholder="请输入糖度"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="天气状况" prop="pickWeather">
        <el-input
          v-model="queryParams.pickWeather"
          placeholder="请输入天气状况"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['grape:pick:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['grape:pick:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['grape:pick:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['grape:pick:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="pickList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="pickId" />
      <el-table-column label="采收日期" align="center" prop="pickTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.pickTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="地块编号" align="center" prop="plotId">
      </el-table-column>
      <el-table-column label="单号" align="center" prop="orderNum" />
      <!-- <el-table-column label="采收品种" align="center" prop="pickSort" /> -->
      <el-table-column label="毛重" align="center" prop="grossWt" />
      <el-table-column label="皮重" align="center" prop="tareWt" />
      <el-table-column label="净重" align="center" prop="netWt" />
      <el-table-column label="扣杂" align="center" prop="lessWt" />
      <el-table-column label="糖度" align="center" prop="brix" />
      <el-table-column label="天气状况" align="center" prop="pickWeather" />
      <el-table-column label="负责人" align="center" prop="pickHead">
        <template #default="scope">
          <dict-tag :options="grape_device_head" :value="scope.row.pickHead ? scope.row.pickHead.split(',') : []"/>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="pickNote" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['grape:pick:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['grape:pick:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改采收信息对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="pickRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="采收日期" prop="pickTime">
          <el-date-picker clearable
            v-model="form.pickTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择采收日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="地块编号" prop="plotId">
          <el-checkbox-group v-model="form.plotId">
            <el-checkbox
              v-for="plot in plotList"
              :key="plot.plotId"
              :label="plot.plotId"
              >
              {{plot.plotId}}
            </el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item label="单号" prop="orderNum">
          <el-input v-model="form.orderNum" placeholder="请输入单号" />
        </el-form-item>
        <!-- <el-form-item label="采收品种" prop="pickSort">
          <el-input v-model="form.pickSort" placeholder="请输入采收品种" />
        </el-form-item> -->
        <el-form-item label="毛重" prop="grossWt">
          <el-input v-model="form.grossWt" placeholder="请输入毛重" />
        </el-form-item>
        <el-form-item label="皮重" prop="tareWt">
          <el-input v-model="form.tareWt" placeholder="请输入皮重" />
        </el-form-item>
        <el-form-item label="净重" prop="netWt">
          <el-input v-model="form.netWt" placeholder="请输入净重" />
        </el-form-item>
        <el-form-item label="扣杂" prop="lessWt">
          <el-input v-model="form.lessWt" placeholder="请输入扣杂" />
        </el-form-item>
        <el-form-item label="糖度" prop="brix">
          <el-input v-model="form.brix" placeholder="请输入糖度" />
        </el-form-item>
        <el-form-item label="天气状况" prop="pickWeather">
          <el-input v-model="form.pickWeather" placeholder="请输入天气状况" />
        </el-form-item>
        <el-form-item label="负责人" prop="pickHead">
          <el-checkbox-group v-model="form.pickHead">
            <el-checkbox
              v-for="dict in grape_device_head"
              :key="dict.value"
              :label="dict.value">
              {{dict.label}}
            </el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item label="备注" prop="pickNote">
          <el-input v-model="form.pickNote" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Pick">
import { listPick, getPick, delPick, addPick, updatePick } from "@/api/grape/pick";

const { proxy } = getCurrentInstance();
const { grape_device_head } = proxy.useDict('grape_device_head');

const pickList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    pickTime: null,
    plotId: [],
    orderNum: null,
    // pickSort: null,
    grossWt: null,
    tareWt: null,
    netWt: null,
    lessWt: null,
    brix: null,
    pickWeather: null,
    pickHead: null,
    pickNote: null
  },
  rules: {
    pickTime: [
      { required: true, message: "采收日期不能为空", trigger: "blur" }
    ],
    plotId: [
      { required: true, message: "地块编号不能为空", trigger: "blur" }
    ],
    orderNum: [
      { required: true, message: "单号不能为空", trigger: "blur" }
    ],
    // pickSort: [
    //   { required: true, message: "采收品种不能为空", trigger: "blur" }
    // ],
    grossWt: [
      { required: true, message: "毛重不能为空", trigger: "blur" }
    ],
    tareWt: [
      { required: true, message: "皮重不能为空", trigger: "blur" }
    ],
    netWt: [
      { required: true, message: "净重不能为空", trigger: "blur" }
    ],
    lessWt: [
      { required: true, message: "扣杂不能为空", trigger: "blur" }
    ],
    brix: [
      { required: true, message: "糖度不能为空", trigger: "blur" }
    ],
    pickWeather: [
      { required: true, message: "天气状况不能为空", trigger: "blur" }
    ],
    pickHead: [
      { required: true, message: "负责人不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询采收信息列表 */
function getList() {
  loading.value = true;
  listPick(queryParams.value).then(response => {
    pickList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}
/** 查询地块基本信息列表,外键 */
import {listPlot} from "@/api/grape/plot";
const plotList = ref([]);
function getPlotList() {
  listPlot("").then(response => {
    plotList.value = response.rows;
    plotList.value.forEach(plot => {
      console.log(plot)
      plot.plotId = plot.plotId.toString()
    });
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    pickId: null,
    pickTime: null,
    plotId: [],
    orderNum: null,
    pickSort: null,
    grossWt: null,
    tareWt: null,
    netWt: null,
    lessWt: null,
    brix: null,
    pickWeather: null,
    pickHead: [],
    pickNote: null
  };
  proxy.resetForm("pickRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.pickId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加采收信息";
  getPlotList();
}

/** 修改按钮操作 */
  function handleUpdate(row) {
  reset();
  const _pickId = row.pickId || ids.value;
  getPick(_pickId).then((response)=>{
    form.value = response.data;
    form.value.pickHead = form.value.pickHead.split(",");
    form.value.plotId = form.value.plotId.split(",");
    console.log(typeof(form.value.plotId[0]))
    console.log(typeof(plotList.value[0].plotId))
    open.value = true;
    title.value = "修改采收信息";
  })
  
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["pickRef"].validate(valid => {
    if (valid) {
      form.value.pickHead = form.value.pickHead.join(",");
      form.value.plotId = form.value.plotId.join(",");
      if (form.value.pickId != null) {
        updatePick(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addPick(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _pickIds = row.pickId || ids.value;
  proxy.$modal.confirm('是否确认删除采收信息编号为"' + _pickIds + '"的数据项？').then(function() {
    return delPick(_pickIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('grape/pick/export', {
    ...queryParams.value
  }, `pick_${new Date().getTime()}.xlsx`)
}

getList();
getPlotList();
</script>
